package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwbsT;
import cn.e4j.modular.crm.service.impl.GwbsTServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 销售回款预测
 * Created by liuw on 2019-9-2.
 */
@Controller
@RequestMapping("/crm/gwat550")
public class Gwat550Controller extends BaseController {

    @Autowired
    private GwbsTServiceImpl gwbsTService;

    /**
     * 跳转销售回款预测管理页面
     */
    @RequiresPermissions("crm:gwat550:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwat550.html";
    }

    /**
     * 查询销售回款预测信息列表
     */
    @RequestMapping("/getGwbslist")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData getGwbslist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbsTService.getGwbslist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData add(GwbsT gwbsT) {
        gwbsTService.add(gwbsT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData edit(GwbsT gwbsT) {
        gwbsTService.edit(gwbsT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData del(GwbsT gwbsT) {
        gwbsTService.delete(gwbsT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbsT> gwbsTList = inDto.getReqList(GwbsT.class);
        gwbsTService.delDataBatch(gwbsTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量更新数据
     */
    @RequestMapping("/updateDataBatch")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData updateDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbsT> gwbsTList = inDto.getReqList(GwbsT.class);
        gwbsTService.updateDataBatch(gwbsTList);
        return SUCCESS_TIP;
    }


    /**
     * 商机编号开窗
     */
    @RequestMapping("/getGwbglist")
    @ResponseBody
    public ResponseData getGwbglist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbsTService.getGwbglist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 行业开窗
     */
    @RequestMapping("/getGwaglist")
    @ResponseBody
    public ResponseData getGwaglist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbsTService.getGwaglist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 省份开窗
     */
    @RequestMapping("/getGwadlist")
    @ResponseBody
    public ResponseData getGwadlist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbsTService.getGwadlist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 产品系列开窗
     */
    @RequestMapping("/getGwaolist")
    @ResponseBody
    public ResponseData getGwaolist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbsTService.getGwaolist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 单位开窗
     */
    @RequestMapping("/getGwablist")
    @ResponseBody
    public ResponseData getGwablist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbsTService.getGwablist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 组织开窗
     */
    @RequestMapping("/getOrgslist")
    @ResponseBody
    public ResponseData getOrgslist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ID",shiroUser.getORG_ID());
        inDto.put("IS_DEL",'0');
        List<Map<String, Object>> result = gwbsTService.getOrgslist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 自动生成
     */
    @RequestMapping("/autoGeneration")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData autoGeneration(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbsT> updateList = new ArrayList<>();
        List<GwbsT> addList = new ArrayList<>();
        try {
            String gwbsdocdate_ = inDto.getString("YEAR_");
            String month_ = inDto.getString("MONTH_").length() == 1
                    ? "0" + inDto.getString("MONTH_")
                    : inDto.getString("MONTH_");
            if (month_.indexOf(".01.03.05.07.08.10.12.") != -1) {
                gwbsdocdate_ += (month_ + "31");
            } else {
                gwbsdocdate_ += (month_ + "30");
            }
            inDto.put("yearMonth_", gwbsdocdate_);

            // 获取拜访提报商品明细
            List<Dto> gwbhlist = gwbsTService.getGwbhList(inDto);
            //Date gwbsdocdate__ = DateUtils.parseDate(gwbsdocdate_, "yyyyMMdd");
            int count = 1;
            for (Dto item : gwbhlist) {
                Dto pa = Dtos.newDto();
                pa.put("GWBSENT", item.getString("GWBHENT"));
                pa.put("GWBSDOCDATE", gwbsdocdate_);
                pa.put("GWBS001", item.getString("GWBHNAME"));
                pa.put("GWBS002", item.getString("GWBHID"));
                pa.put("GWBS003", item.getString("GWBK001"));  // 商品编号
                pa.put("GWBSSEQ", item.getString("GWBKSEQ2"));
                List<GwbsT> gwbsList = gwbsTService.getGwbsByGwbkList(pa); // 查询是否已经有这个回款预测信息
                GwbsT gwbsT = null;
                if (gwbsList.size() == 0) { // 没有相应的回款预测信息，则自动生成
                    gwbsT = new GwbsT();

                    String year1_ = inDto.getString("YEAR_");
                    String month1_ = inDto.getString("MONTH_").length() == 1
                            ? "0" + inDto.getString("MONTH_")
                            : inDto.getString("MONTH_");

                    if (month1_.indexOf(".01.03.05.07.08.10.12.") != -1) {
                        year1_ += (month1_ + "31");
                    } else {
                        year1_ += (month1_ + "30");
                    }
                    year1_ = year1_.substring(0, 4) + "-" + year1_.substring(4, 6) + "-" + year1_.substring(6, 8);
                    pa.put("GWBSDOCDATE", year1_);
                    gwbsT.setGWBSENT(pa.getString("GWBSENT"));
                    gwbsT.setGWBSDOCDATE(LocalDate.parse(pa.getString("GWBSDOCDATE")));
                    gwbsT.setGWBS001(pa.getString("GWBS001"));
                    gwbsT.setGWBS002(pa.getString("GWBS002"));
                    gwbsT.setGWBS003(pa.getString("GWBS003"));
                    gwbsT.setGWBSSEQ(pa.getBigDecimal("GWBSSEQ"));
                    String docno_ = DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS");
                    Random t = new Random();
                    String t1 = String.valueOf(t.nextInt(100));
                    String mum = docno_.substring(0, docno_.length() - 2);
                    /*String numStr = docno_.substring(docno_.length() - 1, docno_.length());
                    String mum = docno_.substring(0, docno_.length() - 1);
                    Integer count1 = count;
                    if (count1.toString().length() > 1) {
                        count1 = Integer.parseInt(count1.toString().substring(count1.toString().length() - 1, count1.toString().length()));
                    }
                    int a = Integer.parseInt(numStr) + count1;
                    Integer b = a;
                    if (b.toString().length() > 1) {
                        b = Integer.parseInt(b.toString().substring(b.toString().length() - 1, b.toString().length()));
                    }*/
                    String docno = "SRYC" + mum + String.valueOf(t1);
                    count++;
                    gwbsT.setGWBSNAME(docno);
                    gwbsT.setGWBS009(item.getString("GWBH016"));//负责经销商
                    gwbsT.setGWBS010(item.getString("GWBH002"));//业务人员
                    gwbsT.setGWBS012(item.getString("YEAR_"));//年度
                    gwbsT.setGWBS013(item.getString("MONTH_"));//期别
                    gwbsT.setGWBS014(item.getString("WEEK_"));
                    gwbsT.setGWBS015(item.getString("GWAK054"));//行业编号
                    gwbsT.setGWBS016(item.getString("GWAK023"));//省份编号
                    gwbsT.setGWBS017(item.getString("GWAM004"));//产品系列
                    //gwbsT.setGWBS018(item.getString("GWBK001"));//
                    //gwbsT.setGWBS019(item.getString("GWBK001"));//
                    //gwbsT.setGWBS020(item.getString("GWBK001"));//实际回款日期

                    gwbsT.setGWBSACTI("N");
                    addList.add(gwbsT);
                }else if ("N".equals(gwbsList.get(0).getGWBSACTI())) {
                    gwbsT = new GwbsT();
                    gwbsT = gwbsList.get(0);
                    updateList.add(gwbsT);
                }

                // 对结果再进行处理
                if (gwbsT != null) {
                    gwbsT.setGWBS004(item.getString("GWBK002"));
                    gwbsT.setGWBS005(item.getBigDecimal("GWBK003"));
                    gwbsT.setGWBS006(item.getBigDecimal("GWBK004"));
                    gwbsT.setGWBS007(item.getBigDecimal("GWBK005"));
                    gwbsT.setGWBS008(item.getString("GWBH031"));//买入几率
                    String gwbs011 = item.getString("GWBK009");
                    if(gwbs011 != "" && gwbs011 != null){
                        gwbs011 = gwbs011.replace("/", "-");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        gwbs011 = sdf.format(sdf.parse(gwbs011));
                        gwbsT.setGWBS011(LocalDate.parse(gwbs011));//预计回款日期
                    }
                }
            }
            gwbsTService.autoGeneration(updateList, addList);
        }catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(500,
                    "自动生成失败");
        }
        return SUCCESS_TIP;
    }
}
